package ui.graphic;

import Modelos.ModeloTabuleiro;
import ui.graphic.InitGUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import static ui.graphic.Frame.isInteger;

/**
 *
 * @author gustavodinis
 */
public class SplashScreen extends JFrame implements Observer {

    ModeloTabuleiro m;
    Image logo;
    URL logoPath;
    InitGUI splash;
    InitMenu menu;

    public SplashScreen(ModeloTabuleiro mT) {
        m = mT;
        setTitle("Eight Minute Empire - The Game");
        setSize(675, 443);
        setPreferredSize(new Dimension(675, 443));
        setResizable(false);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
        splash = new InitGUI();
        splash.setVisible(true);
        menu = new InitMenu(m);
        menu.setVisible(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        fazLayout();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        } finally {
            splash.setVisible(false);
            menu.setVisible(true);
        }

    }

    final void fazLayout() {
        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(splash, BorderLayout.CENTER);
        cp.add(menu, BorderLayout.CENTER);
        cp.validate();
    }

    @Override
    public void update(Observable o, Object arg) {
        repaint();
    }

    public void fazBet() {
        boolean sair = false;

        do {
            if (!m.getActivePlayer().isIa()) {
                String s = (String) JOptionPane.showInputDialog(
                        this,
                        "Jogador " + m.getActivePlayer().getName() + " tem " + m.getActivePlayer().getCoin() + " moedas, quantas moedas aposta?\n",
                        "É altura de apostar",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        null,
                        "");

                if ((s != null) && (s.length() > 0) && isInteger(s) && m.getActivePlayer().getCoin() >= Integer.parseInt(s)) {

                    m.bet(Integer.parseInt(s));
                    sair = true;

                } else {
                    JOptionPane.showMessageDialog(this,
                            "Introduza um numero válido!",
                            "Introdução Inválida",
                            JOptionPane.WARNING_MESSAGE);
                }
            }
        } while (!sair);

    }

}
